package com.linln.admin.system.web;

import com.github.pagehelper.Page;
import com.linln.common.utils.HttpServletUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.dto.ProductDto;
import com.linln.modules.system.entity.BeautyCompany;
import com.linln.modules.system.entity.BeautyProduct;
import com.linln.modules.system.service.CompanyService;
import com.linln.modules.system.vo.admins.ProductCompanyVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(value = "/mains/company")
public class CompanyController extends BaseController{

    @Autowired
    private CompanyService companyService;

    @RequestMapping(value = "/index")
    @RequiresPermissions("mains:company:index")
    public ModelAndView getIndex(Integer pageNum,String phone){
        pageNum = pageNum == null ? super.pageNum : pageNum;
        Integer pageIndex = HttpServletUtil.getParameterInt("size", 1);
        ModelAndView view = new ModelAndView();
        view.setViewName("/mains/company/index");
        Page<BeautyCompany> company = companyService.getIndexCompany(pageNum,pageIndex*10,"");
        view.addObject("list",company.getResult());
        view.addObject("page",company);
        return view;
    }


    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    public String toAdd() {
        return "/mains/company/add";
    }

    @ResponseBody
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public ResultVo saveProduct(BeautyCompany company){
        ResultVo vo = ResultVoUtil.error("操作失败");
        try {
            int end = companyService.saveCompany(company);
            if(end>0){
                vo.setCode(200);
                vo.setMsg("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    @ResponseBody
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public ResultVo updateProduct(BeautyCompany company){
        ResultVo vo = ResultVoUtil.error("操作失败");
        try {
            int end = companyService.updateCompany(company);
            if(end>0){
                vo.setMsg("操作成功");
                vo.setCode(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }


    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") String id, Model model) {
        BeautyCompany company = companyService.getBeautyCompanyById(id);
        model.addAttribute("company", company);
        return "/mains/company/edit";
    }
/**
     * 跳转到编辑页面
     */
    @GetMapping("/detail/{id}")
    public String toDetail(@PathVariable("id") String id, Model model) {
        BeautyCompany company = companyService.getBeautyCompanyById(id);
        model.addAttribute("company", company);
        return "/mains/company/detail";
    }


    @ResponseBody
    @RequestMapping(value = "comList",method = RequestMethod.GET)
    public List<BeautyCompany> getCompanyList(){
        List<BeautyCompany> companyList = companyService.getCompanyList();
        return companyList;
    }

    @ResponseBody
    @RequestMapping(value = "prodList",method = RequestMethod.GET)
    public List<BeautyProduct> getProductList(String comId){
        List<BeautyProduct> products = companyService.getProductList(comId);
        return products;
    }


    @ResponseBody
    @RequestMapping(value = "prodAndCom",method = RequestMethod.GET)
    public List<ProductCompanyVo> getAllProdAndCompany(){
        List<ProductCompanyVo> company = companyService.getAllProdAndCompany();
        return company;
    }

    @ResponseBody
    @RequestMapping(value = "del/{id}")
    public ResultVo deleteCompany(@PathVariable("id") String id){
        ResultVo vo = ResultVoUtil.error("操作失败");
        try {
            int i = companyService.deleteCompany(id);
            if(i>0){
                vo.setMsg("删除成功");
                vo.setCode(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }


}
